//
//  RegisterViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
/*
 by BBS
 */

#import "RegisterViewController.h"



@interface RegisterViewController ()

/**
 电话号码输入框
 */

@property (weak, nonatomic) UITextField *phoneNumberTextField;

/**
 验证码输入框
 */
@property (weak, nonatomic) UITextField *securityCodeTextField;

/**
 获取验证码按钮
 */
@property (weak, nonatomic) UIButton *getSecurityCodeBtn;


/**
 创建承载同意按钮的containerView
 */
@property (weak, nonatomic) UIView *containerView;


/**
 选择是否同意按钮
 */
@property (weak, nonatomic) UIButton *choosedBtn;


/**
 同意label
 */
@property (weak, nonatomic) UILabel *agreeLabel;

/**
 用户协议button
 */
@property (weak, nonatomic) UIButton *userAgreementBtn;

/**
 注册按钮
 */
@property (weak, nonatomic) UIButton *registerBtn;


/**
 倒计时秒数
 */
@property (assign, nonatomic) NSInteger secondsCount;

/**
 计时器
 */
@property (weak, nonatomic) NSTimer *myTimer;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI{
    
    //nav标题
    self.title = @"注册";
    
    //实例化Logo的图片
    UIImageView * imageView =  [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_img_logo_nor.png"]];
    [self.view addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(30);
        make.centerX.equalTo(self.view);
        make.size.equalTo(CGSizeMake(67, 88));
    }];
    
    UIImageView * imageView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_text_logo_nor.png"]];
    [self.view addSubview:imageView2];
    
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.bottom).offset(10);
        make.centerX.equalTo(imageView);
        make.size.equalTo(CGSizeMake(83, 49));
    }];
    
    //--------------------------------------------------
    
    //创建 电话号码输入框 phoneNumberTextField
    UITextField *phoneNumberTextField = [[UITextField alloc] init];
    phoneNumberTextField.placeholder = @"请输入您的手机号码";
    [phoneNumberTextField setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:phoneNumberTextField];
    self.phoneNumberTextField = phoneNumberTextField;
    
    // 约束
    [phoneNumberTextField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(imageView2.bottom).offset(30);
    }];
    
    
    //创建 验证码输入框 securityCodeTextField
    UITextField *securityCodeTextField = [[UITextField alloc] init];
    securityCodeTextField.placeholder = @"请输入验证码";
    [securityCodeTextField setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:securityCodeTextField];
    self.securityCodeTextField = securityCodeTextField;
    
    [securityCodeTextField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneNumberTextField);
        make.top.equalTo(phoneNumberTextField.bottom).offset(20);
        make.right.equalTo(phoneNumberTextField).offset(-90);
    }];
    
    //创建验证码button  getSecurityCodeBtn
    UIButton *getSecurityCodeBtn = [[UIButton alloc] init];
    [getSecurityCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getSecurityCodeBtn.titleLabel setTextColor: [UIColor whiteColor]];
    [getSecurityCodeBtn.titleLabel setFont:kFONT13];
    [getSecurityCodeBtn setBackgroundColor:[UIColor lightGrayColor]];
    getSecurityCodeBtn.layer.cornerRadius = 5;
    getSecurityCodeBtn.layer.masksToBounds = YES;
    [self.view addSubview:getSecurityCodeBtn];
    // 获取验证码点击
    [getSecurityCodeBtn addTarget:self action:@selector(getSecurityCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.getSecurityCodeBtn = getSecurityCodeBtn;
    
    [getSecurityCodeBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(securityCodeTextField);
        make.left.equalTo(securityCodeTextField.right).offset(10);
        make.right.equalTo(phoneNumberTextField.right);
    }];
    
    //--------------------------------------------------
    
    //创建承载同意按钮的containerView
    UIView *containerView = [[UIView alloc] init];
    [self.view addSubview:containerView];
    self.containerView = containerView;
    
    [containerView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(phoneNumberTextField);
        make.top.equalTo(securityCodeTextField.bottom).offset(15);
        make.height.equalTo(30);
    }];
    
    
    //创建选择是否同意按钮 choosedBtn
    UIButton *choosedBtn = [[UIButton alloc] init];
    [choosedBtn setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [choosedBtn setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [self.containerView addSubview:choosedBtn];
    self.choosedBtn = choosedBtn;
    
    //添加点击事件
    [choosedBtn addTarget:self action:@selector(choosedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [choosedBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(containerView.left).offset(80);
        make.centerY.equalTo(containerView);
        make.size.equalTo(CGSizeMake(13, 13));
    }];
    
    //创建同意label  agreeLabel
    UILabel *agreeLabel = [[UILabel alloc] init];
    agreeLabel.text = @"本人同意并阅读";
    agreeLabel.font = kFONT12;
    [self.containerView addSubview:agreeLabel];
    self.agreeLabel = agreeLabel;
    
    [agreeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(choosedBtn.right).offset(5);
        make.centerY.equalTo(containerView);
    }];
    
    //创建用户协议userAgreementBtn
    UIButton *userAgreementBtn = [[UIButton alloc]init];
    [userAgreementBtn setTitle:@"用户协议" forState:UIControlStateNormal];
    [userAgreementBtn.titleLabel setFont:kFONT14];
    [self.containerView addSubview:userAgreementBtn];
    [userAgreementBtn setTitleColor:[UIColor colorWithRed:0.187 green:0.535 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
    //TODO:  这里还可以添加点击事件, textField里的各种选项
    self.userAgreementBtn = userAgreementBtn;
    
    [userAgreementBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(agreeLabel.right).offset(5);
        make.centerY.equalTo(containerView);
    }];
    
    //创建 注册按钮 registerBtn
    UIButton *registerBtn = [[UIButton alloc] init];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn.titleLabel setFont:kFONT16];
    [registerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:kThemeColor];
    registerBtn.layer.cornerRadius = 5;
    registerBtn.clipsToBounds = YES;
    [self.view addSubview:registerBtn];
    //TODO:  添加点击注册之后的逻辑
    self.registerBtn = registerBtn;

    [registerBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(choosedBtn.left);
        make.right.equalTo(userAgreementBtn.right);
        make.height.equalTo(30);
        make.top.equalTo(containerView.bottom).offset(15);
    }];
    
}


//choosedBtn的点击事件
//???:
- (void)choosedBtnClick:(UIButton *)btn{
    
    if (btn.selected) {
        btn.selected = NO;
    }else{
        btn.selected = YES;
    }
}


//获取验证码
- (void)getSecurityCodeBtnClick:(UIButton *)btn{
    
    
    self.secondsCount = 10;
    btn.enabled = NO;
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethed) userInfo:nil repeats:YES];
    
    [self.myTimer fire];
    
    
    
    
}


//获取验证码定时器方法


- (void)timeFireMethed{
    
    
    NSString *labelString = nil;
    
    
    if (self.secondsCount <= 0) {
        
        self.getSecurityCodeBtn.enabled = YES;
        
        labelString = @"获取验证码";
        
        [self.myTimer invalidate];
        self.myTimer = nil;
        
    }else{
        
        self.getSecurityCodeBtn.enabled = NO;
        
        labelString = [NSString stringWithFormat:@"%lds",self.secondsCount];
        
    }
    
    [self.getSecurityCodeBtn setTitle:labelString forState:UIControlStateNormal];
    
    self.secondsCount--;
    
}



@end
